//
//  Home_RootViewController.m
//  FruitHelper
//
//  Created by 赵隆杰 on 2021/12/10.
//

#import "Home_RootViewController.h"
#import "FruitCollectionViewCell.h"
#import "AddFruitViewController.h"
#import "AddToListSheetView.h"

@interface Home_RootViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, strong) UICollectionView *collection;
@property(strong,nonatomic) UICollectionViewFlowLayout *flowLayout;
@property (nonatomic, strong) NSMutableArray *fruits;

@end

@implementation Home_RootViewController

- (NSMutableArray *)fruits
{
    if (!_fruits) {
        _fruits = [NSMutableArray array];
    }
    return _fruits;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.fruits = [NSMutableArray arrayWithArray:[LocalData getAllHomeFruits]];
    [self.collection reloadData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"水果";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"add"] style:UIBarButtonItemStylePlain target:self action:@selector(addAction)];
    
    self.flowLayout = [[UICollectionViewFlowLayout alloc]init];
    self.flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    self.flowLayout.minimumLineSpacing = 30;
    self.flowLayout.minimumInteritemSpacing = 30;
    self.flowLayout.sectionInset = UIEdgeInsetsMake(10, 30, 10, 30);
    self.collection = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:self.flowLayout];
    self.collection.backgroundColor = kBgColor;
    self.collection.delegate = self;
    self.collection.dataSource = self;
    [self.collection registerNib:[UINib nibWithNibName:@"FruitCollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"FruitCollectionViewCell"];
    [self.view addSubview:self.collection];
    
}

- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    UINavigationBar *navigationBar = self.navigationController.navigationBar;
    self.collection.frame = CGRectMake(0, navigationBar.frame.origin.y, SL_WINDOW_WIDTH, SL_WINDOW_HEIGHT - navigationBar.frame.origin.y - SL_VC_TabBar_HEIGHT);
}

#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.fruits.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    FruitCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"FruitCollectionViewCell" forIndexPath:indexPath];
    cell.model = self.fruits[indexPath.row];
    return cell;
}

#pragma mark - UICollectDelegate
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat width = (SL_WINDOW_WIDTH - 60 - 30) / 2;
    CGFloat height = width;
    
    return CGSizeMake(width,height);
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    AddToListSheetView *addToListSheetView = [[AddToListSheetView alloc] initWithFrame:CGRectMake(0, 250, SL_WINDOW_WIDTH, SL_WINDOW_HEIGHT - 250)];
    addToListSheetView.model = self.fruits[indexPath.row];
    [addToListSheetView show];
}

- (void)addAction
{
    
    AddFruitViewController *vc = [[AddFruitViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
    
}


@end
